<?php

class MemosController extends AppController {

    var $name = 'Memos';
    var $helpers = array('Ajax', 'Javascript');

    function index() {
        $memos = $this->Memo->find('all', array(
                    'order' => 'archived ASC, priority DESC'
                ));
        if (empty($memos)) {
            $this->Session->setFlash('There are no memos defined', 'default', array('class' => 'bad'));
        }
        $this->set('memos', $memos);
    }

    function add() {
        if (!empty($this->data)) {
            $user = $this->Auth->user();
            $this->data['Memo']['account_id'] = $user['Account']['id'];
            if ($this->Memo->save($this->data)) {
                //$this->Session->setFlash(__('The memo has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The memo could not be saved. Please, try again.', true));
            }
        }
        $accounts = $this->Memo->Account->find('list');
        $senders = $this->Memo->AccountAsSender->find('list');
        $this->set(compact('accounts', 'senders'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid memo', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Memo->save($this->data)) {
                $this->Session->setFlash(__('The memo has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The memo could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Memo->read(null, $id);
        }
        $accounts = $this->Memo->Account->find('list');
        $accountAsSenders = $this->Memo->AccountAsSender->find('list');
        $this->set(compact('accounts', 'accountAsSenders'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for memo', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Memo->delete($id)) {
            $this->Session->setFlash(__('Memo deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Memo was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    function toArchive($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for memo', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!$this->Memo->saveField('archived', '1')) {
            $this->Session->setFlash(__('Moving to archive failed.', true));
        }
        $this->redirect(array('action' => 'index'));
    }

    function fromArchive($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for memo', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!$this->Memo->saveField('archived', '0')) {
            $this->Session->setFlash(__('Moving back from archive failed.', true));
        }
        $this->redirect(array('action' => 'index'));
    }

}

?>